<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/16
 * Time: 6:42 PM
 */

namespace App\BO\Coupon\CouponProductSkuCheck;

use Illuminate\Support\Facades\Log;

/**
 * 订单最小金额检查
 */
class MinAmountCheck implements ICouponProductSkuCheck
{
    public static function check(CouponProductSkuCheckMeta $checkMeta, \Closure $next)
    {
        $productSkuEntity = $checkMeta->getProductSkuEntity();
        $couponEntity = $checkMeta->getCouponEntity();
        $cartItemEntity = $checkMeta->getCartItemEntity();

        if ($squareMetre = $cartItemEntity->getSquareMetre()) {
            $amount = $productSkuEntity->getPrice() * $squareMetre;
        } else {
            $amount = $productSkuEntity->getPrice();
        }

        if ($amount < $couponEntity->getMinAmount()) {
            Log::info('优惠券' . $couponEntity->getId() . '末达到最小使用金额，不可使用');
            $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
            $checkResultWrapper->setMessage('末达到最小使用金额，不可使用');
            return $checkResultWrapper;
        }

        return $next($checkMeta);
    }
}
